package com.mes.web.controller.system;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.system.domain.SysAutoCodeRule;
import com.mes.system.service.SysAutoCodeRuleService;

/**
 * 编码生成规则表Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-22
 */
@RestController
@RequestMapping("/system/sysAutoCodeRule")
public class SysAutoCodeRuleController extends BaseController {

    @Autowired
    private SysAutoCodeRuleService sysAutoCodeRuleService;

    /**
     * 查询编码生成规则表列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:list')")
    @GetMapping("/list")
    public R<PageInfo> list(SysAutoCodeRule sysAutoCodeRule, HttpServletRequest request, HttpServletResponse response) {
        sysAutoCodeRule.setPage(new PageDomain(request, response));
        return R.data(sysAutoCodeRuleService.findPage(sysAutoCodeRule));
    }

    /**
     * 获取编码生成规则表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:query')")
    @GetMapping(value = "/{id}")
    public R<SysAutoCodeRule> detail(@PathVariable("id") String id) {
        return R.data(sysAutoCodeRuleService.get(id));
    }

    /**
     * 新增编码生成规则表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:add')")
    @Log(title = "编码生成规则表", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  SysAutoCodeRule sysAutoCodeRule) {
        if(!sysAutoCodeRuleService.checkRuleCodeUnique(sysAutoCodeRule)){
            return R.fail("自动编码规则的编号重复");
        }
        if(!sysAutoCodeRuleService.checkRuleNameUnique(sysAutoCodeRule)){
            return R.fail("自动编码规则的名称重复");
        }
        sysAutoCodeRule.setTenantId(SecurityUtils.getTenantId());
        return R.status(sysAutoCodeRuleService.save(sysAutoCodeRule));
    }

    /**
     * 修改编码生成规则表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:edit')")
    @Log(title = "编码生成规则表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SysAutoCodeRule sysAutoCodeRule) {
        if(!sysAutoCodeRuleService.checkRuleCodeUnique(sysAutoCodeRule)){
            return R.fail("自动编码规则的编号重复");
        }
        if(!sysAutoCodeRuleService.checkRuleNameUnique(sysAutoCodeRule)){
            return R.fail("自动编码规则的名称重复");
        }
        return R.status(sysAutoCodeRuleService.save(sysAutoCodeRule));
    }

    /**
     * 删除编码生成规则表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:remove')")
    @Log(title = "编码生成规则表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(sysAutoCodeRuleService.deleteSysAutoCodeRuleByIds(ids));
    }


    /**
     * 导出编码生成规则表列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysAutoCodeRule:export')")
    @Log(title = "编码生成规则表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysAutoCodeRule sysAutoCodeRule) {
        List<SysAutoCodeRule> list = sysAutoCodeRuleService.findList(sysAutoCodeRule);
        ExcelUtil<SysAutoCodeRule> util = new ExcelUtil<SysAutoCodeRule>(SysAutoCodeRule.class);
        return util.exportExcel(list, "编码生成规则表数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
